import { Component, Inject } from '@angular/core';
import { NavController } from 'ionic-angular';
import { TrapPage} from '../newTrap/newTrap'
import { DryPage} from '../newDry/newDry';
import { EnemyPage} from '../newEnemy/newEnemy';
import { DeadtreePage} from '../newDeadTree/newDeadTree';
import { TrackPage} from '../newTrack/newTrack';
import {NewMedicinePage} from '../new-medicine/new-medicine'
import { HttpClient, HttpParams } from "@angular/common/http";
import { NewHomePage} from "../newhome/newhome";
import {NewStaticsPage} from '../new-statics/new-statics';
import { Base } from '../../common/base.js';
import {Platform, Events} from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import {Diagnostic} from '@ionic-native/diagnostic';
import {Network} from '@ionic-native/network'
// import {TrapCache} from '../newTrap/newTrap';
// import {DryCache} from '../newDry/newDry';
// import {EnemyCache} from '../newEnemy/newEnemy';
// import {deadCache} from '../newDeadTree/newDeadTree';
// import {MedCache} from '../new-medicine/new-medicine';
// import {TrackCache} from '../newTrack/newTrack';

@Component({
    selector: 'app-switchProject',
    templateUrl: 'newSwitchProject.html',
})
export class switchProjectPage {
    public trapDisable = false;
    public dryDisable = false;
    public eneDisable = false;
    public medDisable = false;
    public deadDisable = false;
    public trackDisable = false;
    public static Lastlatitude: string;
    public static Lastlongtitude:string;

    public jinriDisable = false;
    public static deviceList = [];

    constructor(private navCtl: NavController, 
        private alertCtrl: AlertController,
        private httpClient: HttpClient, 
        private base: Base,
        public platform: Platform,
        // private trapCache:TrapCache,
        // private dryCache:DryCache,
        // private eneCache:EnemyCache,
        // private deadCache:deadCache,
        // private medCache:MedCache,
        // private trackCache:TrackCache,
        private diagnostic: Diagnostic,
        public network:Network,) {
            //安卓限定
                this.diagnostic.getLocationMode().then((status) => {
                    console.log(status);
                    console.log(this.diagnostic.locationMode.DEVICE_ONLY);
                    
                    if (status == this.diagnostic.locationMode.DEVICE_ONLY) {
                        
                    } else {
                      this.base.showAlert('提示', '请设置为仅限设备', ()=>{this.diagnostic.switchToLocationSettings();});
                    }
                  }).catch((e)=>{alert(e)})

         }

         ionViewWillUnload(){
            localStorage.setItem("111","{'key':'22'}");
            console.log("unload");
        }
        workNum(){
            this.navCtl.push(NewStaticsPage);
        }

         async ionViewWillEnter(){
            this.trapDisable = false;
            this.dryDisable = false;
            this.eneDisable = false;
            this.medDisable = false;
            this.deadDisable = false;
            this.trackDisable = false;

            var cache1:Number = 0;
            var cache2:Number = 0;
            var cache3:Number = 0;
            var cache4:Number = 0;
            var cache5:Number = 0;
            var cache6:Number = 0;
            
            if(localStorage["maintenanceCache"]){
                cache1 = JSON.parse(localStorage["maintenanceCache"]).length;
            }
            if(localStorage["DryCache"]){
                cache2 = JSON.parse(localStorage["DryCache"]).length;
            }
            if(localStorage["enemyCache"]){
                cache3 = JSON.parse(localStorage["enemyCache"]).length;
            }
            if(localStorage["deadCache"]){
                cache4 = JSON.parse(localStorage["deadCache"]).length;
            }
            if(localStorage["medicineCache"]){
                cache5 = JSON.parse(localStorage["medicineCache"]).length;
            }
            if(localStorage["TrackCache"]){
                cache6 = JSON.parse(localStorage["TrackCache"]).length;
            }
            console.log("cache");
            console.log(cache1);

             if(cache1==0 && cache2==0 && cache3==0 && cache4==0 && cache5 == 0 && cache6 == 0){
                 this.jinriDisable = false;
             }else{
                 this.jinriDisable = true;
             }
             console.log(this.jinriDisable);


            //今日工作量
        }

    ionViewDidLoad(){
        this.httpClient.post(this.base.BASE_URL + 'app/getMyDevice', {},
            {
                headers: { token: localStorage['token'] },
                params: new HttpParams({ fromObject: { worker: localStorage['username'] } })
            })
            .subscribe(res => {
                localStorage['device'] = JSON.stringify(res);
            },
                res => {
                    
                })
    }

    trapClick() {
         
        // this.navCtl.push(NewMedicinePage);
        this.trapDisable = true;
        this.navCtl.push(TrapPage);
    }
    dryClick(){
        this.dryDisable = true;
        this.navCtl.push(DryPage);
    }
    deadClick(){
        this.deadDisable = true;
        this.navCtl.push(DeadtreePage);
    }

    medicineClick() {
        //          //控制台输出
        this.medDisable = true;
        this.navCtl.push(NewMedicinePage);
        //this.navCtl.push(TrapPage);
    }
    exitClick(){
        const alert = this.alertCtrl.create({
            title: "警告!",
            subTitle: "是否要退出系统？",
            buttons: [
                {
                    text: '确认', handler: () => {
                    //localStorage.removeItem("token");
                    this.navCtl.pop();
                         
                    }
                },
                {
                    text: '取消', handler: () => {
                         
                    }
                }

        ]
        });
        alert.present();

        // localStorage.removeItem("token");
        // this.navCtl.push(NewHomePage);
    }
    enemyClick(){
        this.eneDisable = true;
        this.navCtl.push(EnemyPage);
    }
    trackClick(){
        this.trackDisable = true;
        this.navCtl.push(TrackPage);
    }
}
